package cn.whit.blog.service.impl;

import java.sql.SQLException;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import cn.whit.blog.dao.UserDao;
import cn.whit.blog.domain.EmailType;
import cn.whit.blog.domain.Tblog;
import cn.whit.blog.domain.User;
import cn.whit.blog.service.TblogService;
import cn.whit.blog.service.UserService;
import cn.whit.blog.util.BeanFactory;
import cn.whit.blog.util.DynamicQuery;
import cn.whit.blog.util.MailUtils;
import cn.whit.blog.util.PageModel;

public class UserServiceImpl implements UserService{
	
	UserDao userDao = (UserDao)BeanFactory.createObject("userDao");

	TblogService tblogService = (TblogService) BeanFactory.createObject("tblogService"); 
	
	@Override
	public PageModel findByPageAndQuery(int startIndex, int pageSize, DynamicQuery query) throws Exception {
		//startIndex 从第一页，索引 1 开始
		int totalRecords = userDao.findTotalRecordsAndQuery(query);
		PageModel pm = new PageModel(startIndex,totalRecords,pageSize);
		List<User> list = userDao.findAllWithPageAndQuery(pm.getStartIndex(),pm.getPageSize(),query);
		pm.setList(list);
		//3_关联url
		pm.setUrl("UserServlet?method=userList");
		return pm;
	}
	
	@Override
	public List<User> findAllWithPageAndQuery(int startIndex, int pageSize, DynamicQuery query) throws Exception {
		return userDao.findAllWithPageAndQuery(startIndex, pageSize, query);
	}

	@Override
	public int findTotalRecordsAndQuery(DynamicQuery query) throws Exception {
		return userDao.findTotalRecordsAndQuery(query);
	}

	@Override
	public  List<User> findByUserNameOrEmail(String userName, String email) throws SQLException {
		return userDao.findByUserNameOrEmail( userName, email);
	}

	@Override
	public void save(User newUser) throws SQLException {
		userDao.save( newUser);
		
		//向用户邮箱发送一份激活邮件
		try {
			 MailUtils.sendMail(newUser.getEmail(),newUser.getActivecode(),EmailType.newUser);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<User> findByUserNameAndPassword(String userName, String password) throws SQLException {
		return userDao.findByUserNameAndPassword( userName,  password);
	}

	@Override
	public List<User> findByActivecode(String activecode) throws SQLException {
		return userDao.findByActivecode( activecode);
	}

	@Override
	public void update(User user) throws SQLException {
		userDao.update(user);
	}

	@Override
	public List<User> findByEmail(String email) throws SQLException {
		return userDao.findByEmail(email);
	}

	@Override
	public void sendEmail(User user, EmailType backpassword,String newpass) {
		//向用户邮箱发送一份包含重置的密码的邮件
		try {
			MailUtils.sendMail(user.getEmail(),"登录用户名是"+ user.getName() + "重置密码为:"+ newpass +
					"请尽快登录系统后休息密码，妥善保存",backpassword);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void deleteById(String s) throws SQLException {
		// 删除用户的时候，需要先把该用户发的博客数据，博客的评论数据，博客的点赞数据等，先删除
		// Service 层是无状态的，就是不管访问的用户是谁，在这里提供对外完的一个完整的业务处理
		
		// s 是待删除的用户的id
		// 1.获取该用户的所有博客，然后调用博客的service来删除博客
		// 2.获取该用户的所有投票，然后调用投票的service来删除投票数据（这里投票的发起者是管理员，不去处理这个步骤了）
		// 3.删除该用户的信息
		List<Tblog> ones = tblogService.findByUserId(s);
		for(Tblog tblog : ones) {
			tblogService.deleteById(tblog.getId());
		}
		userDao.deleteById(s); 
	}

}
